package com.t4.courseware.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;


@Configuration
@Data
@PropertySource("classpath:application-dev.yml")
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliyunConfig {
    // Endpoint
    private String endpoint;

    // 阿里云账号AccessKey
    private String accessKeyId;
    private String accessKeySecret;

    // Bucket
    private String bucketName;

    // 外网访问Bucket域名
    private String urlPrefix;

    // 资源封面根目录
    private String coverRootDic;

    // 资源文件根目录
    private String resourceRootDic;

    // 用户头像根目录
    private String avatarRootDic;

    // 注册用户时的默认头像
    private String defaultAvatarPath;

    // 开启多例模式，默认是单例，由于 OSS 注入到容器后，在后面的代码使用后 shutdown 了，所以再次使用 OSS 就会连接出错
    @Bean
//    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public OSS oss(){
        // 创建OSSClient实例
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }
}
